package com.fengye.algorithms.leecode;

import java.util.Stack;

/**
 * @Author fengye
 * @Date 2023/4/8 11:53
 **/
public class MyQueue232 {
    // 头
    private Stack<Integer> head = null;
    // 尾
    private Stack<Integer> tail = null;
    public MyQueue232() {
        head = new Stack<>();
        tail = new Stack<>();
    }

    public void push(int x) {
        if(head.empty()) {
            head.push(x);
            tail.push(x);
        } else {
            tail.push(x);
            head.clear();
            Stack<Integer> temp = (Stack<Integer>) tail.clone();
            while (!temp.empty()){
                head.push(temp.pop());
            }
        }
    }

    public int pop() {
        if(tail.empty()) {
            return -1;
        }

        int res = head.pop();
        if(head.empty()) {
            tail.pop();
        } else {
            tail.clear();
            Stack<Integer> temp = (Stack<Integer>) head.clone();
            while (!temp.empty()){
                tail.push(temp.pop());
            }
        }
        return res;
    }

    public int peek() {
        if(tail.empty()) {
            return -1;
        }
        return head.peek();
    }

    public boolean empty() {
        return head.empty();
    }

    public static void main(String[] args) {
        MyQueue232 queue = new MyQueue232();
        /**
         * ["MyQueue","push","push","push","push","pop","push","pop","pop","pop","pop"]
         * [[],[1],[2],[3],[4],[],[5],[],[],[],[]]
         */
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.pop();
        queue.push(5);
        queue.pop();
        queue.pop();
        queue.pop();
        queue.pop();
    }
}
